package com.itcncp.common.util;
import com.google.gson.*;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.util.*;
import java.util.Map.Entry;

public class JsonUtils
{ 
    private static Gson gson = new Gson();

    /**
     * 将对象转换成json字符串
     * @param entity
     * @return
     */
	public static <T> String toJson(T entity) {

		return gson.toJson(entity, entity.getClass());		
	
	}

	/**
	 * 将json字符串转换成对象
	 * @param jsonString
	 * @param c
	 * @return
	 */
	public static <T> T toBean(String jsonString, Class<T> c){
	
		return gson.fromJson(jsonString, c); 
	
	}
	 

	/**
	 * 将map转换成对象
	 * @param map
	 * @param c
	 * @return
	 */
	public static <T> T toBean(Map<String,Object> map, Class<T> c){
		
		String jsonString = toJson(map);
		
		return toBean(jsonString, c);
		
	}
	
	/**
	 * 将Map<String,Object>转换成String
	 * @return
	 */
	public static String toString(Map<String,Object> map){
		
		String jsonString = toJson(map);
		
		return jsonString; 
	}
	
	/**
	 * 将Response 的 Map<String,Object>转换成String
	 * @return
     * @param map
	 */
	public static String toMapString(Map<String, String> map){
		
		String jsonString = toJson(map);
		
		return jsonString; 
	}
	 
	/**
	 * 将Json字符串转换成对象集合
	 * @param jsonString
	 * @param c
	 * @return
	 * @throws JsonParseException
	 * @throws IOException
	 */
	public static <T> List<T> toBeanList(String jsonString, Class<T> c) throws JsonParseException, IOException
    {
		 
    	return gson.fromJson(jsonString, new TypeToken<List<T>>(){}.getType());
    	
    }
	
	
	 /** 
     * 获取JsonObject 
     * @param json 
     * @return 
     */  
    public static JsonObject parseJson(String json){
        JsonParser parser = new JsonParser();  
        JsonObject jsonObj = parser.parse(json).getAsJsonObject();   
        return jsonObj;  
    }  
      
    
    /** 
     * 根据json字符串返回Map对象 
     * @param <T>
     * @return 
     */  
    public static <T> HashMap<String,Object> toStringHashMap(T entity){
    	String json = gson.toJson(entity, entity.getClass());
    	
        return toStringHashMap(parseJson(json));  
    }  
    
    /** 
     * 根据json字符串返回Map对象 
     * @param json 
     * @return 
     */  
    public static HashMap<String,Object> toStringHashMap(String json){
        return toStringHashMap(parseJson(json));  
    }  
    
    
	  /** 
     * 将JSONObjec对象转换成Map-List集合 
     * @param json 
     * @return 
     */  
    public static HashMap<String,Object> toStringHashMap(JsonObject json){

		HashMap<String,Object>  map = new HashMap<String,Object>();
        
        Set<Entry<String, JsonElement>> entrySet = json.entrySet();
        
        for (Iterator<Entry<String, JsonElement>> iter = entrySet.iterator(); iter.hasNext(); ){
        
        	Entry<String, JsonElement> entry = iter.next();
            
        	String key = entry.getKey();
            
        	Object value = entry.getValue();
            
        	if(value instanceof JsonArray){
            
        		//map.put((String) key, toList((JsonArray) value));  
            
        	}else if(value instanceof JsonObject){
            
        		//map.put((String) key, toMap((JsonObject) value));  
            
        	} else if (value instanceof Date) {
        		
    			String date =  DateUtils.dateFormat((Date) value);
    			
    			map.put((String) key, date);
    			
    		} else if (value instanceof String) {
        		
        		map.put((String) key,(String)value);
        	
        	}else{
        		
        		//字符串替换双引号
        		value = value.toString().replace("\"","");
				map.put((String) key, value.toString());
        	}
         
        }  
        return map;  
    }  
    

    public static Map<String,Object> toMap(String json){
        return toMap(parseJson(json));  
    }  
    
    /** 
     * 将JSONObjec对象转换成Map-List集合 
     * @param json 
     * @return 
     */  
    public static Map<String, Object> toMap(JsonObject json){
        
    	Map<String, Object> map = new HashMap<String, Object>();
        
        Set<Entry<String, JsonElement>> entrySet = json.entrySet();
        
        for (Iterator<Entry<String, JsonElement>> iter = entrySet.iterator(); iter.hasNext(); ){
        
        	Entry<String, JsonElement> entry = iter.next();
            
        	String key = entry.getKey();
            
        	Object value = entry.getValue();
            
        	if(value instanceof JsonArray){
            
        		map.put((String) key, toList((JsonArray) value));
            
        	}else if(value instanceof JsonObject){
            
        		map.put((String) key, toMap((JsonObject) value));
            
        	}else{
        		
        		map.put((String) key, value);
        	
        	}
                
        }  
        return map;  
    }  
    
    /** 
     * 将JSONArray对象转换成List集合 
     * @param json 
     * @return 
     */  
    public static List<Object> toList(JsonArray json){
        List<Object> list = new ArrayList<Object>();
        for (int i=0; i<json.size(); i++){  
            Object value = json.get(i);
            if(value instanceof JsonArray){  
                list.add(toList((JsonArray) value));  
            }  
            else if(value instanceof JsonObject){  
                list.add(toMap((JsonObject) value));  
            }  
            else{  
                list.add(value);  
            }  
        }  
        return list;  
    }   
} 

